package com.locationtracker.plus.ui.fragment.login

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.locationtracker.plus.app.appViewModel
import com.locationtracker.plus.app.base.BaseFragment
import com.locationtracker.plus.app.eventViewModel
import com.locationtracker.plus.app.ext.showMessage
import com.locationtracker.plus.app.util.CacheUtil
import com.locationtracker.plus.app.util.CommonUtil.countDownCoroutines
import com.locationtracker.plus.databinding.FragmentLoginBinding
import com.locationtracker.plus.viewmodel.request.RequestLoginRegisterViewModel
import com.locationtracker.plus.viewmodel.state.LoginRegisterViewModel
import kotlinx.coroutines.Job
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.parseState

class LoginFragment : BaseFragment<LoginRegisterViewModel, FragmentLoginBinding>() {

    private var mCountdownJob: Job? = null
    private val requestLoginRegisterViewModel by viewModels<RequestLoginRegisterViewModel>()

    override fun initView(savedInstanceState: Bundle?) {
        addLoadingObserve(requestLoginRegisterViewModel)
        mDatabind.vm = mViewModel
        CacheUtil.getLastLoginPhone().let {
            if (it.isNotEmpty()) mViewModel.phone.set(it)
        }

        mDatabind.click = ProxyClick()
    }

    override fun createObserver() {
        requestLoginRegisterViewModel.loginResult.observe(viewLifecycleOwner) { resultState ->
            parseState(resultState, {
                mCountdownJob?.cancel()
                //登录成功 通知账户数据发生改变鸟
                CacheUtil.setUser(it)
                appViewModel.userInfo.value = it
                eventViewModel.loginEvent.value = true
                nav().navigateUp()
            }, {
                //登录失败
                showMessage(it.errorMsg)
            })
        }
        requestLoginRegisterViewModel.getCodeStatus.observe(viewLifecycleOwner) {
            mCountdownJob = countDownCoroutines(60, lifecycleScope, {
                mViewModel.second.set("${it}s")
            }, {
                mViewModel.showButton.set(false)
            }, {
                mViewModel.showButton.set(true)
            })
        }
    }

    inner class ProxyClick {
        fun close() {
            nav().navigateUp()
        }

        fun login() {
            when {
                mViewModel.phone.get().isEmpty() -> showMessage("Pls Input Phone Number")
                mViewModel.code.get().isEmpty() -> showMessage("Pls Input Verification")
                else -> requestLoginRegisterViewModel.loginReq(
                    mViewModel.phone.get(),
                    mViewModel.code.get()
                )
            }
        }

        fun getCode() {
            when {
                mViewModel.phone.get().isEmpty() -> showMessage("Pls Input Phone Number")
                else -> requestLoginRegisterViewModel.getCode(mViewModel.phone.get(), mDatabind.ccp.selectedCountryCode)
            }
        }
    }
}